package com.jftone.bh.app.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.jftone.bh.app.model.*;
import org.apache.log4j.Logger;
import org.jftone.annotation.Autowired;
import org.jftone.annotation.Service;
import org.jftone.annotation.Transactional;
import org.jftone.dao.Dao;
import org.jftone.exception.ComponentException;
import org.jftone.exception.DaoException;
import org.jftone.util.DataMap;
import org.jftone.util.IData;


@Service
public class SysRoleService {
	
	private Logger logger = Logger.getLogger(SysRoleService.class);
	
	@Autowired
	private Dao dao;
	
	public List<SysRole> querySysRoles(IData<String, Object> inData) throws ComponentException{
		List<SysRole> sysRoleList = null;
		try{
			sysRoleList = dao.query(SysRole.class, inData);
		}catch(DaoException e){
			logger.error("查询系统角色数据异常" + e.getMessage(), e);
			throw new ComponentException("查询系统角色数据异常" + e.getMessage(), e);
		}
		return sysRoleList;
	}
	
	public SysRole getSysRole(IData<String, Object> inData) throws ComponentException{
		SysRole sysRole = null;
		try{
			sysRole = dao.get(SysRole.class, inData);
		}catch(DaoException e){
			logger.error("查询系统角色数据异常" + e.getMessage(), e);
			throw new ComponentException("查询系统角色数据异常" + e.getMessage(), e);
		}
		return sysRole;
	}
	
	@Transactional
	public int saveSysRole(IData<String, Object> data, boolean updateFlag) throws ComponentException{
		int resCode = 0;
		SysRole sysRole = new SysRole();
		sysRole.setParentCode(data.getString("parentCode"));
		sysRole.setRoleName(data.getString("roleName"));
		sysRole.setRemark(data.getString("remark", ""));
		try{
			if(updateFlag){
				sysRole.setId(Integer.parseInt(data.getString("roleId")));
				dao.update(sysRole);
			}else{
				IData<String, Object> inData = new DataMap<String, Object>();
				inData.put("roleCode", data.getString("roleCode"));
				if(dao.count(SysRole.class, inData)>0){
					return 1;
				}
				sysRole.setRoleLevel(data.getShort("roleLevel"));
				sysRole.setRoleCode(data.getString("roleCode"));
				sysRole.setCreateTime(new Date());
				dao.save(sysRole);
			}
		}catch(DaoException e){
			logger.error("保存系统角色数据异常" + e.getMessage(), e);
			throw new ComponentException("保存系统角色数据异常" + e.getMessage(), e);
		}
		return resCode;
	}
	
	@Transactional
	public int deleteRole(int roleId) throws ComponentException{
		int resultCode = 0;
		try{
			IData<String, Object> inData = new DataMap<String, Object>();
			inData.put("id", roleId);
			SysRole role = dao.get(SysRole.class, inData);
			if(role == null){
				return 1;
			}
			String roleCode = role.getRoleCode();
			inData.clear();
			inData.put("parentCode", roleCode);
			if(dao.count(SysRole.class, inData)>0){
				return 2;
			}
			inData.clear();
			inData.put("roleCode",roleCode);
			if(dao.count(SysAdminRole.class, inData)>0){
				return 3;
			}
			//删除所有配置权限
			dao.delete(SysRight.class, inData);
			dao.delete(SysRightNode.class, inData);
			dao.delete(role);
		}catch(DaoException e){
			logger.error("删除菜单数据错误："+e.getMessage(), e);
			throw new ComponentException("删除菜单数据错误："+e.getMessage(), e);
		}
		return resultCode;
	}
	
	public List<SysRight> querySysRight(IData<String, Object> inData) throws ComponentException{
		List<SysRight> sysRightList = null;
		try{
			sysRightList = dao.query(SysRight.class, inData);
		}catch(DaoException e){
			logger.error("查询角色权限数据错误："+e.getMessage(), e);
			throw new ComponentException("查询角色权限数据错误："+e.getMessage(), e);
		}
		return sysRightList;
	}
	
	public List<SysMenu> querySysMenu(IData<String, Object> inData) throws ComponentException{
		List<SysMenu> sysMenuList = null;
		try{
			sysMenuList = dao.query(SysMenu.class, inData);
		}catch(DaoException e){
			logger.error("查询系统菜单数据错误："+e.getMessage(), e);
			throw new ComponentException("查询系统菜单数据错误："+e.getMessage(), e);
		}
		return sysMenuList;
	}
	
	public List<IData<String, Object>> queryMenuByModul(IData<String, Object> inData) throws ComponentException{
		List<IData<String, Object>> retList = null;
		try{
			retList = dao.query("queryMenuByModul", inData);
		}catch(DaoException e){
			logger.error("查询系统模块数据错误："+e.getMessage(), e);
			throw new ComponentException("查询系统模块数据错误："+e.getMessage(), e);
		}
		return retList;
	}
	
	public List<IData<String, Object>> queryFuncByMenu(IData<String, Object> inData) throws ComponentException{
		List<IData<String, Object>> retList = null;
		try{
			retList = dao.query("queryFuncByMenu", inData);
		}catch(DaoException e){
			logger.error("查询菜单功能数据错误："+e.getMessage(), e);
			throw new ComponentException("查询菜单功能数据错误："+e.getMessage(), e);
		}
		return retList;
	}
	
	/**
	 * 保存角色权限数据
	 * @param roleCode
	 * @param srList
	 * @return
	 * @throws ComponentException
	 */
	@Transactional
	public int saveRoleRight(String roleCode, String moduleCode, List<SysRight> srList) throws ComponentException{
		int resultCode = 0;
		try{
			//删除角色菜单
			IData<String, Object> inData = new DataMap<String, Object>();
			inData.put("roleCode", roleCode);
			inData.put("moduleCode", moduleCode);
			List<SysRightNode> tmpList = dao.query(SysRightNode.class, inData);
			if(null!=tmpList && tmpList.size()>0){
				List<IData<String, Object>> paramList = new ArrayList<IData<String, Object>>();
				for(SysRightNode tmp : tmpList){
					IData<String, Object> data = new DataMap<String, Object>();
					data.put("NODE_CODE", tmp.getNodeCode());
					data.put("ROLE_CODE", roleCode);
					paramList.add(data);
				}
				dao.updateBatch("deleteRightByNodeAndRole", paramList);
				dao.delete(SysRightNode.class, inData);
			}
			
			if(null != srList && srList.size()>0){
				IData<String, Integer> tmpNode = new DataMap<String, Integer>();
				List<SysRightNode> srnList = new ArrayList<SysRightNode>();
				String code = null;
				for(SysRight right : srList){
					code = right.getNodeCode();
					//过滤已经存在的记录
					if(tmpNode.containsKey(code)) continue;
					tmpNode.put(code, 1);		
					
					SysRightNode srn = new SysRightNode();
					srn.setRoleCode(roleCode);
					srn.setModuleCode(moduleCode);
					srn.setNodeCode(code);
					srnList.add(srn);
				}
				dao.insertBatch(srnList);
				dao.insertBatch(srList);
			}
		}catch(DaoException e){
			logger.error("更新角色权限数据错误："+e.getMessage(), e);
			throw new ComponentException("更新角色权限数据错误："+e.getMessage(), e);
		}
		return resultCode;
	}
	
	public List<SysAdminRole> querySysAdminRole(IData<String, Object> inData) throws ComponentException{
		List<SysAdminRole> retList = null;
		try{
			retList = dao.query(SysAdminRole.class, inData);
		}catch(DaoException e){
			logger.error("查询角色权限数据错误："+e.getMessage(), e);
			throw new ComponentException("查询角色权限数据错误："+e.getMessage(), e);
		}
		return retList;
	}
	
	public List<IData<String, Object>> getRightByRoleCode(List<IData<String, Object>> list) throws ComponentException{
		List<IData<String, Object>> retList = null;
		try{
			retList = dao.queryUnion("getRightByRoleCode", list, false);
		}catch(DaoException e){
			throw new ComponentException("查询用户角色权限异常" + e.getMessage());
		}
		return retList;
	}

}
